package com.xhwl.centiotdata.suppliersystem.guangtuo;

import cn.hutool.http.HttpException;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xhwl.common.exception.ApplicationException;
import com.xhwl.common.exception.BusinessException;
import com.xhwl.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * @author zhouyongzhou
 * @date 2023/04/21  14:00
 */
@Service
public class GeneralGuangTuoAlarmHostHandler {
    private static final Logger log = LoggerFactory.getLogger(GeneralGuangTuoAlarmHostHandler.class);


    public JSONObject get(String path) {
        String result = null;
        try {
            result = HttpRequest
                    .get(path)
                    .execute()
                    .body();
        } catch (HttpException e) {
            log.error("【GuangTuo get】path:{}; request failed!", path);
            e.printStackTrace();
            throw new ApplicationException();
        }
        if (StringUtils.isEmpty(result)) {
            log.error("【GuangTuo get】path:{};result is null!", path);
            throw new BusinessException();
        }
        log.info("【GuangTuo get】path:{};result:{}", path, result);
        return JSON.parseObject(result);
    }

    public JSONObject put(String path) {
        String result = null;
        try {
            result = HttpRequest.put(path).execute()
                    .body();
        } catch (HttpException e) {
            log.error("【GuangTuo put】path:{}; request failed!", path);
            e.printStackTrace();
            throw new ApplicationException();
        }
        if (StringUtils.isEmpty(result)) {
            log.error("【GuangTuo put】path:{};result is null!", path);
            throw new BusinessException();
        }
        log.info("【GuangTuo put】path:{};result:{}", path, result);
        return JSON.parseObject(result);
    }
}
